// pages/addAddress/addAddress.js
import request from '../../request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token:"",
    name: "",
    phone: "",
    detailaddress: "",
    address: [],
    selectoraddress: '请选择',
    warn1:false,
    warn2:false,
    warn3:false,
    warn4:false,
  },
  addresschecked(e) { //地址三级联动
    this.setData({address:e.detail.value})
    let address = e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
    this.setData({
      selectoraddress: address
    })

  },
  getname(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getphone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getdetailaddress(e) {
    this.setData({
      detailaddress: e.detail.value
    })
  },
  submit() { //提交新增地址
   let that=this
   let isok1=true//符合规范
   let isok2=true//符合规范
   let isok3=true//符合规范
   let isok4=true//符合规范
    if(this.data.name.replace(/\s+/g,"")==""){
      isok1=false
    }
    //校验手机号码
    if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(this.data.phone)){
      isok2=false
    }
    //校验所在地区
    if(this.data.address.length==0){
      isok3=false
    }
    //校验详细地址
    if(this.data.detailaddress.replace(/\s+/g,"")==""){
      isok4=false
    }
   
//校验成功 
    if(isok1&&isok2&&isok3&&isok4){
      let that=this
      request({
        url: 'http://api_dev.wanxikeji.cn/api/userAddressAddModify',
        data:{
          token:that.data.token,
          phone:that.data.phone,
          procince:that.data.address[0],
          city:that.data.address[1],
          city:that.data.address[1],
          area:that.data.address[2],
          name:that.data.name,
          detailed:that.data.detailaddress,
        },
        success:res=>{
          // console.log(res)
          that.data.eventChannel.emit("addrSuccess",{data:res.data})
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }else if(isok1==false){
      that.setData({warn1:true})
    }
    else if(isok2==false){
      that.setData({warn2:true})
    }
    else if(isok3==false){
      that.setData({warn3:true})
    }
    else if(isok4==false){
      that.setData({warn4:true})
    }
   
    console.log(isok)
  },
  focus(){
    this.setData({
      warn1:false,
      warn2:false,
      warn3:false,
      warn4:false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */ 

  onLoad: function (options) {
    let that=this
    const eventChannel = that.getOpenerEventChannel()
    that.setData({eventChannel})
    wx.getStorage({
      key: 'token',
      success:res=>{
        that.setData({token:res.data})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})